package com.xwd.servlet;

import com.xwd.listener.MySessionActivationListener;
import com.xwd.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ClassName LoginInServlet
 * @Description: com.xwd.servlet
 * @Auther: xiwd
 * @Date: 2022/2/12 - 02 - 12 - 21:11
 * @version: 1.0
 */
@WebServlet(
        name = "loginservlet",
        urlPatterns = {
                "/loginservlet.do"
        }
)
public class LoginServlet  extends HttpServlet{
        //methods
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                //super.doGet(req, resp);
                this.doPost(req, resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                //super.doPost(req, resp);
                //获取请求参数
                req.setCharacterEncoding("UTF-8");
                String username = req.getParameter("username");
                String password = req.getParameter("password");
                //user instance
                User user = new User(username,password);
                //session
                HttpSession session = req.getSession();
                session.setAttribute("user",user);
                //绑定HttpSessionActivationListener监听器对象
                session.setAttribute("sessionactivationlistener",new MySessionActivationListener());

        }
}
